package com.example.lin.zyfypt_327ljd.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.ActionMenuItemView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lin.zyfypt_327ljd.R;
import com.example.lin.zyfypt_327ljd.iface.CollectListener;
import com.example.lin.zyfypt_327ljd.iface.FocusListener;
import com.example.lin.zyfypt_327ljd.model.CollectModel;
import com.example.lin.zyfypt_327ljd.model.FocusModel;

//网络相关部分import时ResponseBody是okhttp3，其他的都是retrofit2
public class ViewVideoActivity0 extends AppCompatActivity implements View.OnClickListener {
    private int resid,idolid;
    private int userid;
    Context context;

    private Boolean flagcollect=false;//收藏标志
    private Boolean flagfocus=false;//关注标志
    private CollectModel collectmodel;//收藏model
    private FocusModel focusmodel;//关注model
    private SharedPreferences sp;//简单存储
    private String sessionID="";  //sessionid

    private Button btndetail;
    private TextView name,date,description,author;
    private String na="",da="",de="",au="";

    CollectListener listener=new CollectListener() {
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            ActionMenuItemView item=(ActionMenuItemView)findViewById(R.id.menucollect);
            //根据mode中response返回的字符串区分返回结果
            switch (msg)
            {
                case "2": System.out.println("----收藏成功");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "1":System.out.println("----收藏失败");
                    break;
                case "5":System.out.println("----取消收藏成功");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                case "4":System.out.println("----取消收藏失败");
                    break;
                case "7":System.out.println("----已收藏");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "8":System.out.println("----未收藏");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };
    FocusListener listener2=new FocusListener() {
        @Override
        public void onResponse(String msgs) {
            ActionMenuItemView item=(ActionMenuItemView)findViewById(R.id.menufocus);
            switch (msgs)
            {
                case "2": System.out.println("----关注成功");
                    flagfocus=true;
                    item.setTitle("取消关注");
                    break;
                case "1":System.out.println("----关注失败");
                    break;
                case "5":System.out.println("----取消关注成功");
                    flagfocus=false;
                    item.setTitle("关注");
                    break;
                case "4":System.out.println("----取消关注失败");
                    break;
                case "7":System.out.println("----已关注");
                    flagfocus=true;
                    item.setTitle("取消关注");
                    break;
                case "8":System.out.println("----未关注");
                    flagfocus=false;
                    item.setTitle("关注");
                    break;
                default:
                    Toast.makeText(context, msgs, Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msgs) {
            Toast.makeText(context, msgs, Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_video0);
        System.out.println("----查看视频详情");
        init();
        na=getIntent().getStringExtra("name");
        da=getIntent().getStringExtra("update_time");
        de=getIntent().getStringExtra("description");
        au=getIntent().getStringExtra("author");

        name.setText(na);
        date.setText(da);
        description.setText(de);
        author.setText(au);

        context=ViewVideoActivity0.this;
        resid  = getIntent().getIntExtra("resid",1);//获取传递的资源id
        idolid = getIntent().getIntExtra("idolid",1);
        userid = getIntent().getIntExtra("userid",7);//获取传递的资源用户id

        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        readSP();//读取sessionid
    }
    private void readSP() {
        sessionID=sp.getString("sessionid",null);
    }
    private void init() {
        name =(TextView)findViewById(R.id.textView01);
        date =(TextView)findViewById(R.id.textView11);
        description =(TextView)findViewById(R.id.textView21);
        author=(TextView)findViewById(R.id.textView61);

        btndetail = (Button) findViewById(R.id.button);
        btndetail.setOnClickListener(this);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);//加载菜单布局
        collectmodel=new CollectModel();//实例化对象
        collectmodel.exist("video",resid,sessionID,listener);//判断是否收藏
        focusmodel=new FocusModel();
        focusmodel.exist("userfocus",idolid,sessionID,listener2);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.menucollect:
                //Toast.makeText(this, "文章收藏", Toast.LENGTH_SHORT).show();
                if(flagcollect)//如果已收藏，则调用取消收藏
                {
                    System.out.println("----准备取消收藏");
                    collectmodel.uncollect("video",resid,sessionID,listener);
                }
                else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备收藏");
                    collectmodel.collect("video",resid,sessionID,listener);
                }
                break;
            case R.id.menufocus:
                //Toast.makeText(this, "文章关注", Toast.LENGTH_SHORT).show();
                if(flagfocus)//如果已关注，则调用取消关注
                {
                    System.out.println("----准备取消关注");
                    focusmodel.unfocus("userfocus",idolid,sessionID,listener2);
                }
                else//如果未关注，则调用关注
                {
                    System.out.println("----准备关注");
                    focusmodel.focus("userfocus",idolid,sessionID,listener2);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button:
                Intent intent=getIntent();
                intent.getStringExtra("videopath");
                intent.setClass(ViewVideoActivity0.this,ViewVideoActivity.class);
                startActivity(intent);
                break;
        }
    }
}
